1679C - Rooks Defenders - CodeForces Solution


data structures implementation *1400

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.buffer.readline


class FenwickTree:
	def __init__(self, n):
		self.n = n
		self.fenwick_tree = [0 for _ in range(self.n + 1)]

	def update(self, i, v):
		i += 1

		while i <= self.n:
			self.fenwick_tree[i] += v
			i += i & -i

	def get_sum(self, i):
		s = 0
		i += 1

		while i > 0:
			s += self.fenwick_tree[i]
			i -= i & -i

		return s

def func():
	r = [0 for __ in range(n+1)]
	c = [0 for __ in range(n+1)]

	row = FenwickTree(n + 1)
	col = FenwickTree(n + 1)

	for i in range(q):
		t, *inp = list(map(int, input().split()))
		if t == 1:
			x, y = inp
			if r[y] == 0:
				row.update(y, 1)
			if c[x] == 0:
				col.update(x, 1)
			r[y] += 1
			c[x] += 1

		elif t == 2:
			x, y = inp
			if r[y] == 1:
				row.update(y, -1)
			if c[x] == 1:
				col.update(x, -1)
			r[y] -= 1
			c[x] -= 1

		else:
			x1, y1, x2, y2 = inp
			if row.get_sum(y2) - row.get_sum(y1 - 1) == y2 - y1 + 1:
				print('YES')
			elif col.get_sum(x2) - col.get_sum(x1 - 1) == x2 - x1 + 1:
				print('YES')
			else:
				print('NO')


n, q = map(int, input().split())
func()

C++ Code:

#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 2e5+10;

struct node{
	int l,r;
	int v;
	int sum;// 和 
}tr[4*N],trr[4*N];

void build(struct node tr[],int u, int l, int r)
{
	tr[u] = {l,r};
	if(l == r) return ;
	int mid = l + r >> 1;
	build(tr,u << 1,l,mid),build(tr,u << 1 | 1,mid + 1,r);
}

void pushup(struct node tr[],int u)
{
	tr[u].sum = tr[u<<1].sum + tr[u<<1|1].sum;
}

int query(struct node tr[],int u, int l, int r)
{
	if(tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
	
	int sum = 0;
	int mid = tr[u].l + tr[u].r >> 1;
	if(l <= mid) sum += query(tr,u << 1,l,r);
	if(mid < r) sum += query(tr,u << 1 | 1,l,r);
	return sum;
}

void modify(struct node tr[],int u, int x, int v)
{
	if(tr[u].l == x && tr[u].r == x) 
	{
		tr[u].v += v;
		tr[u].sum = (tr[u].v > 0);
	}
	else
	{
		int mid = tr[u].l + tr[u].r >> 1;
		if(x <= mid) modify(tr,u << 1, x, v);
		else modify(tr,u << 1 | 1, x, v);
		pushup(tr,u);
	}
}

void solve()
{
	int n,q;
	cin >> n >> q;
	build(tr,1,1,n), build(trr,1,1,n);
	for(int i = 0; i < q; i ++)
	{
		int t;
		scanf("%lld",&t);
		if(t == 1)
		{
			int x,y;
			scanf("%lld %lld",&x,&y);
			modify(tr,1,x,1);
			modify(trr,1,y,1);
		}
		else if(t == 2)
		{
			int x,y;
			scanf("%lld %lld",&x,&y);
			modify(tr,1,x,-1);
			modify(trr,1,y,-1);
		}
		else
		{
			int x,y,xx,yy;
			scanf("%lld %lld %lld %lld",&x,&y,&xx,&yy);
			int cntx = query(tr,1,x,xx), cnty = query(trr,1,y,yy);
			if(cntx == xx - x + 1 || cnty == yy - y + 1) printf("Yes\n");
			else printf("No\n");
		}
	}
}

int main()
{
   int T = 1;
//   cin >> T;
   while(T--) solve();
} 


Comments

Submit
0 Comments
More Questions

1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam